<?php

namespace app\supply\logic;

use app\supply\logic\BasicLogic;
use think\facade\Db;

class SupplyLogic extends BasicLogic
{

    /**
     * @desc    供应商列表
     * @date    2024/7/03
     * @author  luckyXiong
     */
    public function getSupplyList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $supply_id       = getIsset($params,'supply_id');
        $spu             = getIsset($params,'spu');
        $is_head         = getIsset($params,'is_head');
        $is_1688         = getIsset($params,'is_1688');
        $create_time     = getIsset($params,'create_time');
        $where           = [];
        if($supply_id){
            $where[] = ['s.id', '=', $supply_id];
        }

        if($is_head){
            $where[] = ['s.is_head', '=',$is_head];
        }

        if($is_1688){
            $where[] = ['s.is_1688', '=',$is_1688];
        }

        $where[] = ['s.status', '=',1];

        if($spu){
            $where[] = ['i.spu', '=',$spu];
        }

        if (!empty($create_time)) {
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        $field = "s.id, s.supply_name,s.status,s.is_1688,s.is_head, s.created_time,i.spu";
        $res   = Db::name('supply')
            ->alias('s')
            ->leftJoin('supply_info i', 's.id = i.supply_id')
            ->field($field)
            ->where($where)
            ->group('i.supply_id')
            ->order('s.is_head ASC, s.created_time DESC')
            ->paginate($limit);

        $list = $res->items()?:[];


        if(!$spu){
            foreach($list as $k=>$v){
                $list[$k]['spu'] = self::get_skus($v['id']);
            }
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    供应商编辑列表
     * @date    2024/7/05
     * @author  luckyXiong
     */
    public function editSupplyList($params=[]){
          $id           = $params['id']??'';
          $supply_name  = $params['supply_name']??'';
          $is_head      = $params['is_head']??'';
          $is_1688      = $params['is_1688']??'';
          $status       = $params['status']??'1';
          Db::name('supply')
            ->where('id',$id)
            ->update(
                [
                'supply_name'   => $supply_name,
                'is_head'       => $is_head,
                'status'        => $status,
                'is_1688'       => $is_1688,
                'created_time'  => date("Y-m-d H:i:s", time()),
                ]
            );
        return  $this->jsonMessage(200,true,'编辑成功',null);

    }


    /**
     * @desc    获取sku信息
     * @date    2024-10-22
     * @author  luckyXiong
     */
    public function get_skus($supply_id){
           if(!$supply_id)  return '';
           $list = Db::name('supply_info')->where('supply_id',$supply_id)->column('spu');
           return  $list ? implode(',',$list) : '';
    }


    /**
     * @desc    供应商新增接口
     * @date    2024/7/17
     * @author  luckyXiong
     */
    public function addSupplyList($params=[]){
        $supply_name  = $params['supply_name']??'';
        $is_head      = $params['is_head']??'';
        $is_1688      = $params['is_1688']??'';
        if(!$supply_name){
            return  $this->jsonMessage(201,false,'供应商名称不能为空!',null);
        }

        $exist_info   = Db::name('supply')->where(['supply_name'=>$supply_name])->find();
        if($exist_info)  return  $this->jsonMessage(201,false,'供应商已存在!',null);

        Db::name('supply')->insert(
            [
                'supply_name'  =>$supply_name,
                'is_head'      =>$is_head,
                'is_1688'      =>$is_1688,
                'created_time' =>date("Y-m-d H:i:s", time()),
            ]
        );
        return  $this->jsonMessage(200,true,'新增成功',null);
    }



    /**
     * @desc    供应商编辑列表
     * @date    2024/7/05
     * @author  luckyXiong
     */
    public function findSupplyList($params=[]){
        $id           = $params['id']??'';
        $data         = Db::name('supply')->where('id',$id)->find();
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    获取供应商下拉列表
     * @date    2024/7/17
     * @author  luckyXiong
     */
    public function get_supply_list($params=[]){
          $spu             = $params['spu']??'';
          $supply_name     = $params['supply_name']??'';
          $where           = [];
          if($supply_name){
            $supply_name = trim($supply_name);
            $where[] = ['s.supply_name', 'like', '%' . $supply_name . '%'];
          }

          if($spu){
            $spu = trim($spu);
            $where[] = ['i.spu', 'like', '%' . $spu . '%'];
          }
          $field = 's.id,s.supply_name,i.spu';
          $list = Db::name('supply')
                  ->alias('s')
                  ->leftJoin('supply_info i', 's.id = i.supply_id')
                  ->field($field)
                  ->where($where)
                  ->group('i.supply_id')
                  ->select()
                  ->toArray();
          return  $this->jsonMessage(200,true,'获取成功',$list);
    }


    /**
     * @desc    获取供应商下拉列表
     * @date    2024/7/17
     * @author  luckyXiong
     */
    public function get_supplys($params=[]){
        $spu             = $params['spu']??'';
        $supply_name     = $params['supply_name']??'';
        $where           = [];
        if($supply_name){
            $supply_name = trim($supply_name);
            $where[] = ['s.supply_name', 'like', '%' . $supply_name . '%'];
        }

        if($spu){
            $spu = trim($spu);
            $where[] = ['i.spu', 'like', '%' . $spu . '%'];
        }
        $field = 'i.id,s.supply_name,i.spu,i.supply_link';
        $list = Db::name('supply_info')
            ->alias('i')
            ->leftJoin('supply s', 's.id = i.supply_id')
            ->field($field)
            ->where($where)
            ->select()
            ->toArray();
        foreach($list as $k=>$v){
            $list[$k]['supply_name'] = $v['spu'].'-'.$v['supply_name'];
        }
        return  $this->jsonMessage(200,true,'获取成功',$list);
    }




    /**
     * @desc    供应商spu关系列表
     * @date    2024/10/22
     * @author  luckyXiong
     */
    public function get_spu_info($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $supply_id       = getIsset($params,'supply_id');
        $where           = [];
        if($spu){
            $where[] = ['i.spu', '=',$spu];
        }
        $where[] = ['i.supply_id', '=',$supply_id];
        $field   = 'i.id,s.supply_name,s.id as supply_id,i.remark,i.supply_link,i.spu,i.created_time';
        $res = Db::name('supply_info')
            ->alias('i')
            ->leftJoin('supply s', 's.id = i.supply_id')
            ->field($field)
            ->where($where)
            ->order('created_time DESC')
            ->paginate($limit);

        $list = $res->items()?:[];
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    编辑供应商spu关系列表
     * @date    2024/10/22
     * @author  luckyXiong
     */
    public function edit_spu_list($params=[]){
       $id          = $params['id']??'';
       $spu         = $params['spu']??'';
       $supply_link = $params['supply_link']??'';
       $remark      = $params['remark']??'';
        if(!$supply_link){
            return  $this->jsonMessage(201,false,'供应商链接不能为空!',null);
        }

        if(!$spu){
            return  $this->jsonMessage(201,false,'spu不能为空!',null);
        }
        Db::name('supply_info')->where('id',$id)->update(
            [
                'spu'        =>$spu,
                'supply_link'=>$supply_link,
                'remark'     =>$remark,
            ]
        );
        return  $this->jsonMessage(200,true,'操作成功',null);
    }



    /**
     * @desc    添加供应商spu关系列表
     * @date    2024/10/22
     * @author  luckyXiong
     */
    public function add_spu_list($params=[]){
        $supply_id   = $params['supply_id']??'';
        $spu         = $params['spu']??'';
        $supply_link = $params['supply_link']??'';
        $remark      = $params['remark']??'';

        if(!$supply_id){
            return  $this->jsonMessage(201,false,'供应商ID不能为空!',null);
        }

        if(!$supply_link){
            return  $this->jsonMessage(201,false,'供应商链接不能为空!',null);
        }

        if(!$spu){
            return  $this->jsonMessage(201,false,'spu不能为空!',null);
        }

        $exist_info  = Db::name('supply_info')->where(['supply_link'=>$supply_link,'spu'=>$spu])->find();

        if($exist_info){
            return  $this->jsonMessage(201,false,'同一spu下的链接已存在!',null);
        }

        Db::name('supply_info')->insert(
                [
                    'supply_id'    =>$supply_id,
                    'supply_link'  =>$supply_link,
                    'spu'          =>$spu,
                    'remark'       =>$remark,
                    'created_time' =>date("Y-m-d H:i:s", time()),
                ]
        );
        return  $this->jsonMessage(200,true,'操作成功',null);
    }



    /**
     * @desc    获取供应商删除接口
     * @date    2024/7/17
     * @author  luckyXiong
     */
    public function Deletelist($params=[]){
           $id = $params['id']??'';
           if(!$id) {
               return  $this->jsonMessage(201,false,'ID不能为空',null);
           }

          Db::name('supply_info')->where(['id'=>$id])->delete();
          return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc    供应商删除接口
     * @date    2024/10/24
     * @author  luckyXiong
     */
    public function delete_supply_list($params=[]){
        $id = $params['id']??'';
        if(!$id) {
            return  $this->jsonMessage(201,false,'ID不能为空',null);
        }

        $exist_spu =Db::name( 'supply_info')->where(['supply_id'=>$id])->find();
        if($exist_spu) {
            return  $this->jsonMessage(201,false,'该供应商下存在绑定的spu,请先删除供应商对应绑定的spu!',null);
        }
        Db::name( 'supply')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功',null);
    }














}